﻿using Ingr.SP3D.Common.Middle;
using Ingr.SP3D.Route.Middle;
using System;
using System.Collections.ObjectModel;
using System.Data;

namespace SWS.PIP.DATA
{
    public class GasPipeFabData
    {
		/// <summary>
		/// 获取排烟管管材部件加工数据,仅处理包含一个一刀切虾壳弯管段
		/// 其他管段会返回异常
		/// </summary>
		/// <param name="oBO">PipeStockPart部件</param>
		/// <returns></returns>
		public static DataTable GetGasPipeStockPartFabricateData(BusinessObject oBO)
		{
			//校验输入对象
			if (oBO == null)
			{
				throw new ArgumentNullException("输入对象为空。");
			}

			PipeStockPart oPipeStockPart = oBO as PipeStockPart;
			if (oPipeStockPart == null)
			{
				throw new Exception($"输入对象({oBO.ObjectID})不是管材部件。");
			}

			//返回表格列名
			DataTable resultDT = new DataTable();
			resultDT.Columns.Add("oid");
			resultDT.Columns.Add("L1");
			resultDT.Columns.Add("R1");
			resultDT.Columns.Add("L2");
			resultDT.Columns.Add("R2");

			//计算加工数据
			string sOid = oBO.ObjectID;

			//判断管材部件是否符合信息提取要求（仅包含1刀切，且数量为1的管段）
			int iMiterpCount = 0;
			PipeTurnFeature oMiterTurnFeature = null;
			PipeStraightFeature oPipeStraightFeature = null;
			PipeTurnFeature oPipeTurnFeature = null;
			ReadOnlyCollection<RouteFeature> oFeatures = oPipeStockPart.Features;
			foreach (RouteFeature oRouteFeature in oFeatures)
			{
				oPipeTurnFeature = oRouteFeature as PipeTurnFeature;
				if (oPipeTurnFeature != null && oPipeTurnFeature.NumberOfMiters == 1)
				{
					iMiterpCount++;
					oMiterTurnFeature = oPipeTurnFeature;
				}
			}
			if (iMiterpCount == 0)
			{
				throw new Exception($"管件{oPipeStockPart.ObjectID}不包含切1刀的虾壳弯。");
			}
			else if (iMiterpCount > 1)
			{
				throw new Exception($"管件{oPipeStockPart.ObjectID}包含{iMiterpCount}个切1刀的虾壳弯，无法处理数量超过1个切1刀的虾壳弯管材。");
			}

			//获取Miter的长度及角度,管半径
			double dMiterLength = oMiterTurnFeature.Length;
			double dBendAngle = oMiterTurnFeature.BendAngle;
			double dNPD = oMiterTurnFeature.NPD.Size/1000;

			//获取Miter前段长度
			double iPrePipeLength = 0.0;
			int iMiterIndex = oFeatures.IndexOf(oMiterTurnFeature);
			for (int i = 0; i < iMiterIndex; i++)
			{
				oPipeStraightFeature = oFeatures[i] as PipeStraightFeature;
				oPipeTurnFeature = oFeatures[i] as PipeTurnFeature;
				if (oPipeStraightFeature != null)
				{
					iPrePipeLength += oPipeStraightFeature.Length;
				}
				else if (oPipeTurnFeature != null)
				{
					iPrePipeLength += oPipeTurnFeature.Length;
				}
			}

			//获取Miter后段长度
			double iNextPipeLength = 0.0;
			for (int i = iMiterIndex + 1; i < oFeatures.Count; i++)
			{
				oPipeStraightFeature = oFeatures[i] as PipeStraightFeature;
				oPipeTurnFeature = oFeatures[i] as PipeTurnFeature;
				if (oPipeStraightFeature != null)
				{
					iNextPipeLength += oPipeStraightFeature.Length;
				}
				else if (oPipeTurnFeature != null)
				{
					iNextPipeLength += oPipeTurnFeature.Length;
				}
			}

			//计算前后端增减
			double iValue = Math.Tan(dBendAngle / 2) * dNPD / 2;

			DataRow row = resultDT.NewRow();
			row["oid"] = sOid;
			row["L1"] = Math.Round(iPrePipeLength + dMiterLength / 2 + iValue, 3);
			row["R1"] = Math.Round(iPrePipeLength + dMiterLength / 2 - iValue, 3);
			row["L2"] = Math.Round(iNextPipeLength + dMiterLength / 2 + iValue, 3);
			row["R2"] = Math.Round(iNextPipeLength + dMiterLength / 2 - iValue, 3);
			resultDT.Rows.Add(row);

			return resultDT;
		}
	}
}
